'''
Created on Aug 21, 2009

@author: Roni
'''
import cliques.mdp.limitedSampling
import random
class OfflineLimitedSampling(cliques.mdp.limitedSampling.LimitedSampling):
    '''
    classdocs
    '''


    def __init__(self, k, edge_prob, num_of_nodes):
        '''
        Constructor
        '''
        self.real_graph = None
        self.noise = 0
        cliques.mdp.limitedSampling.LimitedSampling.__init__(self, k, edge_prob, num_of_nodes)        

    def set_real_graph(self,real_graph):
        ''' Set the real graph, create the edge prob matrix using the noise field '''
        self.real_graph=real_graph
        
        # Assert that nodes int() is according to index (nodes are unicode indices)
        nodes= real_graph.nodes()
        indices= list(xrange(len(nodes)))
        for i in xrange(len(nodes)):
            if i!=int(nodes[i]):
                raise NameError("Nodes not sequential integer %s!=%d" % (i, int(nodes[i])))
        
        # Create random matrix
        self.edge_prob_matrix = []
        for row in xrange(0,len(nodes)):
            neighbors_prob = []
            for column in xrange(0,len(nodes)):
                random_noise = random.random()*self.noise
                if real_graph.has_edge(nodes[row],nodes[column]):
                    neighbors_prob.append(1-random_noise)
                else:
                    neighbors_prob.append(random_noise)
            self.edge_prob_matrix.append(neighbors_prob)
            
#    def pr(self, node, neighbor):
#        return self.edge_prob_matrix[int(node)][int(neighbor)]
    
    def pr(self,node,neighbor):
        return self.edge_prob_matrix[node][neighbor]
    